unit cef_ref_print_settings;

{$mode objfpc}{$H+}
{$i cef.inc}

interface

{$ifdef cef_trunk}
uses
    cef3api, cef3intf, cef3types, cef3lib,
    cef_ref_base;

type
    { TCefPrintSettingsRef }

    TCefPrintSettingsRef = class(TCefBaseRef, ICefPrintSettings)
    private
        function instance: PCefPrintSettings; inline;
    protected
        function IsValid: boolean;
        function IsReadOnly: boolean;
        function Copy: PCefPrintSettings;
        procedure SetOrientation(ALandscape: boolean);
        function IsLandscape: boolean;
        procedure SetPrinterPrintableArea(APhysicalSizeDeviceUnits: TCefSize;
            APrintableAreaDeviceUnits: TCefRect; ALandscapeNeedsFlip: boolean);
        procedure SetDeviceName(const AName: ustring);
        function GetDeviceName: ustring;
        procedure SetDPI(ADPI: integer);
        function GetDPI: integer;
        procedure SetPageRanges(ARanges: TPageRanges);
        function GetPageRangesCount: Integer;
        function GetPageRanges: TPageRanges;
        procedure SetSelectionOnly(ASelectionOnly: Boolean);
        function IsSelectionOnly: Boolean;
        procedure SetCollate(ACollate: Boolean);
        function WillCollate: Boolean;
        procedure SetColorModel(AModel: TCefColorModel);
        function GetColorModel: TCefColorModel;
        procedure SetCopies(ACopies: Integer);
        function GetCopies: Integer;
        procedure SetDuplexMode(AMode: TCefDuplexMode);
        function GetDuplexMode: TCefDuplexMode;
    public
        class function UnWrap(Data: Pointer): ICefPrintSettings;
    end;
{$endif cef_trunk}

implementation

{$ifdef cef_trunk}

{ TCefPrintSettingsRef }

function TCefPrintSettingsRef.instance: PCefPrintSettings;
begin
    Result := PCefPrintSettings(FData);
end;

function TCefPrintSettingsRef.IsValid: boolean;
begin
    Result := instance^.is_valid(FData) <> 0;
end;

function TCefPrintSettingsRef.IsReadOnly: boolean;
begin
    Result := instance^.is_read_only(FData) <> 0;
end;

function TCefPrintSettingsRef.Copy: PCefPrintSettings;
begin
    Result := instance^.copy(FData);
end;

procedure TCefPrintSettingsRef.SetOrientation(ALandscape: boolean);
begin
    instance^.set_orientation(FData, integer(ALandscape));
end;

function TCefPrintSettingsRef.IsLandscape: boolean;
begin
    Result := instance^.is_landscape(FData) <> 0;
end;

procedure TCefPrintSettingsRef.SetPrinterPrintableArea(APhysicalSizeDeviceUnits: TCefSize;
    APrintableAreaDeviceUnits: TCefRect; ALandscapeNeedsFlip: boolean);
begin
    instance^.set_printer_printable_area(
        FData, @APhysicalSizeDeviceUnits, @APrintableAreaDeviceUnits,
        integer(ALandscapeNeedsFlip));
end;

procedure TCefPrintSettingsRef.SetDeviceName(const AName: ustring);
var
    csn: TCefString;
begin
    csn := CefString(AName);
    instance^.set_device_name(FData, @csn);
end;

function TCefPrintSettingsRef.GetDeviceName: ustring;
begin
    Result := CefStringFreeAndGet(instance^.get_device_name(FData));
end;

procedure TCefPrintSettingsRef.SetDPI(ADPI: integer);
begin
    instance^.set_dpi(FData, ADPI);
end;

function TCefPrintSettingsRef.GetDPI: integer;
begin
    Result := instance^.get_dpi(FData);
end;

procedure TCefPrintSettingsRef.SetPageRanges(ARanges: TPageRanges);
var
    pr, ppr: PCefPageRange;
    i: integer;
begin
    pr := GetMem(Length(ARanges) * SizeOf(TCefPageRange));
    try
        ppr := pr;
        for i := High(ARanges) to Low(ARanges) do
        begin
            Move(ARanges[i], ppr^, SizeOf(TCefPageRange));
            {$warning Check is it really increments pointer using size of type instead of 1}
            Inc(ppr);
        end;
        instance^.set_page_ranges(FData, Length(ARanges), pr);
    finally
        Freemem(pr);
    end;
end;

function TCefPrintSettingsRef.GetPageRangesCount: Integer;
begin
    Result := instance^.get_page_ranges_count(FData);
end;

function TCefPrintSettingsRef.GetPageRanges: TPageRanges;
var
    cnt: TSize;
    rng: PCefPageRange;
begin
    instance^.get_page_ranges(FData, @cnt, rng);
    SetLength(Result, cnt);
    Move(rng^, Result, cnt * SizeOf(TCefPageRange));
end;

procedure TCefPrintSettingsRef.SetSelectionOnly(ASelectionOnly: Boolean);
begin
    instance^.set_selection_only(FData, Integer(ASelectionOnly));
end;

function TCefPrintSettingsRef.IsSelectionOnly: Boolean;
begin
    Result := Boolean(instance^.is_selection_only(FData));
end;

procedure TCefPrintSettingsRef.SetCollate(ACollate: Boolean);
begin
    instance^.set_collate(FData, Integer(ACollate));
end;

function TCefPrintSettingsRef.WillCollate: Boolean;
begin
    Result := Boolean(instance^.will_collate(FData));
end;

procedure TCefPrintSettingsRef.SetColorModel(AModel: TCefColorModel);
begin
    instance^.set_color_model(FData, AModel);
end;

function TCefPrintSettingsRef.GetColorModel: TCefColorModel;
begin
    Result := instance^.get_color_model(FData);
end;

procedure TCefPrintSettingsRef.SetCopies(ACopies: Integer);
begin
    instance^.set_copies(FData, ACopies);
end;

function TCefPrintSettingsRef.GetCopies: Integer;
begin
    Result := instance^.get_copies(FData);
end;

procedure TCefPrintSettingsRef.SetDuplexMode(AMode: TCefDuplexMode);
begin
    instance^.set_duplex_mode(FData, AMode);
end;

function TCefPrintSettingsRef.GetDuplexMode: TCefDuplexMode;
begin
    Result := instance^.get_duplex_mode(FData);
end;

class function TCefPrintSettingsRef.UnWrap(Data: Pointer): ICefPrintSettings;
begin
    if Data <> nil then
        Result := Create(Data) as ICefPrintSettings
    else
        Result := nil;
end;

{$endif cef_trunk}

end.
